package com.zzzfwd.menuhelper

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.widget.addTextChangedListener
import androidx.recyclerview.widget.RecyclerView

class ConfigurationAdapter(
    private val context: Context, private val configurationList: ArrayList<Configuration>
) : RecyclerView.Adapter<ConfigurationAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val checkBox: CheckBox = view.findViewById(R.id.check_box)
        val dishNameEdit: EditText = view.findViewById(R.id.dish_name_edit)
        val priceEdit: EditText = view.findViewById(R.id.price_edit)
        val minWeightEdit: EditText = view.findViewById(R.id.min_weight_edit)
        val maxWeightEdit: EditText = view.findViewById(R.id.max_weight_edit)
        val deleteBtn: Button = view.findViewById(R.id.delete_btn)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.configuration_item, parent, false)
        val holder = ViewHolder(view)
        holder.checkBox.setOnClickListener {
            val configuration = configurationList[holder.adapterPosition]
            val isSelected = configuration.selected
            configuration.selected = !isSelected
        }
        holder.dishNameEdit.addTextChangedListener {
            val configuration = configurationList[holder.adapterPosition]
            configuration.dish.name = it.toString()
        }
        holder.priceEdit.addTextChangedListener {
            val configuration = configurationList[holder.adapterPosition]
            configuration.dish.price = it.toString().toIntOrNull() ?: 0
        }
        holder.minWeightEdit.addTextChangedListener {
            val configuration = configurationList[holder.adapterPosition]
            configuration.dish.minWeight = it.toString().toIntOrNull() ?: 0
        }
        holder.maxWeightEdit.addTextChangedListener {
            val configuration = configurationList[holder.adapterPosition]
            configuration.dish.maxWeight = it.toString().toIntOrNull() ?: 0
        }
        holder.deleteBtn.setOnClickListener {
            AlertDialog.Builder(context)
                .setTitle("提示")
                .setMessage("确定删除该项？")
                .setPositiveButton("是") { _, _ ->
                    val position = holder.adapterPosition
                    configurationList.removeAt(position)
                    notifyItemRemoved(position)
                    notifyDataSetChanged()
                    Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show()
                }
                .setNegativeButton("否") { _, _ ->
                    Toast.makeText(context, "取消删除", Toast.LENGTH_SHORT).show()
                }
                .show()
        }
        return holder
    }

    override fun getItemCount(): Int = configurationList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val configuration = configurationList[position]
        holder.checkBox.isChecked = configuration.selected
        holder.dishNameEdit.setText(configuration.dish.name)
        holder.priceEdit.setText(configuration.dish.price.toString())
        holder.minWeightEdit.setText(configuration.dish.minWeight.toString())
        holder.maxWeightEdit.setText(configuration.dish.maxWeight.toString())
    }
}